/*
 *
 */

package org.darkforest.dao.dao.impl;

import org.darkforest.dao.dao.GeneralDao;
import org.darkforest.dao.mapper.GeneralMapper;

import java.util.List;


/**
 * 数据库公共类Dao类接口实现类
 */

public class GeneralDaoImpl<T,E>  extends BaseDaoImpl<T,E> implements GeneralDao<T,E> {

    protected Class<E> mapperClass;
    /**
     * 设置对应的MapperClass
     */
    @Override
    public void setMapperClass(Class<E> mapperClass)
    {
        super.mapperClass = mapperClass;
    }

    /**
     * 得到对应的Mapper对象
     */
    public E getMapper()
    {
        return super.sqlSessionTemplate.getConfiguration().getMapper(mapperClass, getSqlSession());
    }

    public GeneralMapper<T> getGeneralMapper()
    {
        return (GeneralMapper<T>)this.getMapper();
    }

    @Override
    public List<T> selectByCondition(T object) {
        try {
            mapperClass = (Class<E>) Class.forName("org.darkforest.dao.mapper."+object.getClass().getSimpleName()+"Mapper");
            setMapperClass(mapperClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return getGeneralMapper().selectByCondition(object);
    }
}
